package com.block.api.controller.product;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.annotate.NeedToken;
import com.block.common.bean.CommonParam;
import com.block.common.bean.SessionBean;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.model.dto.UserInfoDTO;
import com.block.common.exception.ApiException;
import com.block.common.security.util.ApiUtil;
import com.block.common.util.AESOperator;
import com.block.common.vo.ResponseVo;
import com.block.db.common.vo.BaseUserInfoVo;
import com.block.db.entity.AppEncrypt;
import com.block.product.model.vo.ProductInfoVO;
import com.block.product.service.IProductInfoService;
import com.block.product.service.IProductUnionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/product")
@Validated
public class ProductController {

    @Autowired
    IProductInfoService productInfoService;
    @Autowired
    IProductUnionService productUnionService;

    /**
     * 查询产品列表
     */
    @NeedToken
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
    public String productList(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        // 用户信息
        Integer sceneId = tranData.getInteger("sceneId");
        String aesKey = tranData.getString("aesKey");
        Long uid = sessionBean.getUid();
        String mobile = sessionBean.getMobile();

        BaseUserInfoVo userInfoVo = new BaseUserInfoVo();
        userInfoVo.setUid(uid);
        userInfoVo.setMobile(mobile);
        userInfoVo.setIp(ip);
        userInfoVo.setUserType(sessionBean.getUserType());
        userInfoVo.setChannelCode(sessionBean.getChannelCode());
        userInfoVo.setChannelName(sessionBean.getChannelName());
        userInfoVo.setReChannelCode(commonParam.getChannelCode());
        userInfoVo.setReChannelName(commonParam.getChannelName());
        userInfoVo.setReChannelType(commonParam.getChannelType());
        userInfoVo.setZhima(sessionBean.getZhima());

        JSONObject extData = commonParam.getExtData();
        String userAgent = extData.getString("userAgent");

        List<ProductInfoVO> resultList =  productInfoService.getProductInfoList(commonParam.getClientType(), sceneId, userInfoVo, userAgent);

        // TODO: 2023/4/26 产品信息为空的时候，进行消息通知
        // 根据接口参数是否有 asekey，判断是否进行产品数据加密；防止产品数据被扒
        JSONObject jsonObject = new JSONObject();
        if (StrUtil.isEmpty(aesKey)){
            jsonObject.put("dataList", resultList);
            jsonObject.put("encrypt", false);
        }else{
            try {
                String result = AESOperator.aesEncrypt(JSON.toJSONString(resultList), aesKey);
                jsonObject.put("dataList", result);
                jsonObject.put("encrypt", true);
            }catch (Exception exception){
                exception.printStackTrace();
                throw new ApiException("产品列表加密异常");
            }
        }

        ResponseVo responseVo =  new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

    /**
     * 获取联登产品信息
     * 联登产品的登陆操作
     */
    @NeedToken
    @RequestMapping(value = "/unionInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String unionInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                              @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                              @RequestAttribute(value = "uri") String uri,
                              @RequestAttribute(value = "commonParam") CommonParam commonParam,
                              @RequestAttribute(value = "startDate") Date startdate,
                              @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                              @RequestAttribute(value = "ip") String ip) throws ApiException {

        // 用户信息
        Long productId = tranData.getLong("productId");
        Long uid = sessionBean.getUid();
        String mobile = sessionBean.getMobile();

        BaseUserInfoVo userInfoVo = new BaseUserInfoVo();
        userInfoVo.setUid(uid);
        userInfoVo.setMobile(mobile);
        userInfoVo.setChannelCode(commonParam.getChannelCode());

        UserInfoDTO userInfoDTO = new UserInfoDTO();
        userInfoDTO.setUid(sessionBean.getUid());
        userInfoDTO.setChannelCode(sessionBean.getChannelCode());
        userInfoDTO.setReqChannelCode(commonParam.getChannelCode());
        userInfoDTO.setClientType(commonParam.getClientType());
        userInfoDTO.setIp(ip);

        ResponseVo responseVo =  productUnionService.findProductUnionInfo(productId, userInfoVo, userInfoDTO);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

}
